﻿/********************************************************************
	created:	2011/05/12
	created:	12:5:2011   20:35
	filename: 	D:\My Documents\Visual Studio 2010\Projects\IntranetMessager\ServiceLibrary\ServiceRepository.cs
	file path:	D:\My Documents\Visual Studio 2010\Projects\IntranetMessager\ServiceLibrary
	file base:	ServiceRepository
	file ext:	cs
	author:		邢远扬、黄能、邱文军
	
	purpose:	接口定义，基础功能类的内部成员声明，以及其构造函数
*********************************************************************/
using System.ServiceModel;
using CommonLibrary;

namespace ServiceLibrary
{
    public interface IClientCallBack
    {
        [OperationContract(IsOneWay = true)]
        void SendBack(byte[] message);
    }

    [ServiceContract(CallbackContract = typeof (IClientCallBack), SessionMode = SessionMode.Required)]
    public interface IServiceRepository
    {
        [OperationContract(IsOneWay = true)]
        void SendData(byte[] message);
    }

    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
    public partial class ServiceRepository
    {
        #region Delegates

        public delegate void LogDelegate(string log);

        #endregion

        private static LogDelegate _logHander;
        internal static KeyProvider MyKeyProvider;
        internal static OnlineUserRepository OnlineUsers;

        public ServiceRepository(LogDelegate method)
        {
            _logHander = method;
            DataManagement.DatabaseTest();
            MyKeyProvider = new KeyProvider();
            OnlineUsers = new OnlineUserRepository();
            MessageHandlerListInit();

            Printf("Server Init OK");
        }
    }

    internal static class ServiceFunctions
    {
        internal static byte[] SealMessageToEnvelope<T>(T msgContent, int uid,
                                                        bool isDesEncrypt, bool isRsaSign)
        {
            var envelope = new Protocol.MessageEnvelope
                               {
                                   MessageType = msgContent.GetType(),
                                   MyUid = uid,
                                   ByteMessage = Functions.SerializeToBytes(msgContent)
                               };

            if (uid != -1)
            {
                if (isDesEncrypt)
                {
                    string userKey, userIv;
                    KeyProvider.GetUserKey(uid, out userKey, out userIv);
                    envelope.ByteMessage =
                        CryptoService.DesEncrypt(userKey, userIv, envelope.ByteMessage);
                }
            }

            envelope.HashCode = Functions.MessageHash(envelope.ByteMessage);

            if (!isRsaSign)
                envelope.Signed = false;
            else
            {
                envelope.HashCode = CryptoService.RsaSignHash(envelope.HashCode);
                envelope.Signed = true;
            }

            return Functions.SerializeToBytes(envelope);
        }
    }
}